DVWA之 Command Injection

参考链接:http://www.freebuf.com/articles/web/116714.html

Command Injection

Command Injection(命令注入),指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一。

LOW级别

服务器核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php 

if( isset( $_POST[ 'Submit' ] ) ) {

// Get input

$target = $_REQUEST[ 'ip' ];

// Determine OS and execute the ping command.

if( stristr( php_uname( 's' ), 'Windows NT' ) ) {

// Windows

$cmd = shell_exec( 'ping ' . $target );

}

else {

// *nix

$cmd = shell_exec( 'ping -c 4 ' . $target );

}

// Feedback for the end user

echo "<pre>{$cmd}</pre>";

}

?>

  • php_uname(mode)
    这个函数会返回运行php的操作系统的相关描述,参数mode可取值”a” (此为默认,包含序列”s n r v m”里的所有模式),”s ”(返回操作系统名称),”n”(返回主机名),” r”(返回版本名称),”v”(返回版本信息), ”m”(返回机器类型)。
  • stristr(string,search,before_search),具体自己百度

    实战

    window和linux系统都可以用&&来执行多条命令
    输入内容可以为
  • 127.0.0.1&&ls
  • 127.0.0.1&&whoami
  • 127.0.0.1&&net user

下面图以第二个为例

image

可以看到最下面爆出了当前使用用户

Medium级别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php 

if( isset( $_POST[ 'Submit' ] ) ) {

// Get input

$target = $_REQUEST[ 'ip' ];

// Set blacklist

$substitutions = array(

'&&' => '',

';' => '',

);

// Remove any of the charactars in the array (blacklist).

$target = str_replace( array_keys( $substitutions ), $substitutions, $target );

// Determine OS and execute the ping command.

if( stristr( php_uname( 's' ), 'Windows NT' ) ) {

// Windows

$cmd = shell_exec( 'ping ' . $target );

}

else {

// *nix

$cmd = shell_exec( 'ping -c 4 ' . $target );

}

// Feedback for the end user

echo "<pre>{$cmd}</pre>";

}

?>

可以看到增加了str_replace()函数,将&&和;这两个字符过滤成空字符了,但还是有些字符可以用,如&

1
127.0.0.1&ls

image

这里注意&与&&的区别:

1
2
3
Command 1&&Command 2

(先执行Command 1,执行成功后执行Command 2,否则不执行Command 2)

1
2
3
Command 1&Command 2

先执行Command 1,不管是否成功,都会执行Command 2

也可以用如下绕过(过滤成一个空白字符)

1
127.0.0.1&;&ipconfig

image

这是因为”127.0.0.1&;&ipconfig”中的” ;”会被替换为空字符,这样一来就变成了”127.0.0.1&& ipconfig” ,会成功执行。

High级别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php 

if( isset( $_POST[ 'Submit' ] ) ) {

// Get input

$target = trim($_REQUEST[ 'ip' ]);

// Set blacklist

$substitutions = array(

'&' => '',

';' => '',

'| ' => '',

'-' => '',

'$' => '',

'(' => '',

')' => '',

'`' => '',

'||' => '',

);

// Remove any of the charactars in the array (blacklist).

$target = str_replace( array_keys( $substitutions ), $substitutions, $target );

// Determine OS and execute the ping command.

if( stristr( php_uname( 's' ), 'Windows NT' ) ) {

// Windows

$cmd = shell_exec( 'ping ' . $target );

}

else {

// *nix

$cmd = shell_exec( 'ping -c 4 ' . $target );

}

// Feedback for the end user

echo "<pre>{$cmd}</pre>";

}

?>

可以看出,相比Medium级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。

实战

黑名单看似过滤了所有的非法字符,但仔细观察到是把”| ”(注意这里|后有一个空格)替换为空字符,于是 ”|”成了“漏网之鱼”。

1
2
3
Command 1 | Command 2

“|”是管道符,表示将Command 1的输出作为Command 2的输入,并且只打印Command 2执行的结果。

参考链接:http://blog.sina.com.cn/s/blog_70bea87d010129dv.html

输入如下命令:

1
127.0.0.1|net user

image

impossible

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php 

if( isset( $_POST[ 'Submit' ] ) ) {

// Check Anti-CSRF token

checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

// Get input

$target = $_REQUEST[ 'ip' ];

$target = stripslashes( $target );

// Split the IP into 4 octects

$octet = explode( ".", $target );

// Check IF each octet is an integer

if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {

// If all 4 octets are int's put the IP back together.

$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];

// Determine OS and execute the ping command.

if( stristr( php_uname( 's' ), 'Windows NT' ) ) {

// Windows

$cmd = shell_exec( 'ping ' . $target );

}

else {

// *nix

$cmd = shell_exec( 'ping -c 4 ' . $target );

}

// Feedback for the end user

echo "<pre>{$cmd}</pre>";

}

else {

// Ops. Let the user name theres a mistake

echo '<pre>ERROR: You have entered an invalid IP.</pre>';

}

}

// Generate Anti-CSRF token

generateSessionToken();

?>

首先使用 stripslashes 转义反斜线后的字符串,然后恐怖的来了,将输入的字符串依据’.’号分隔开,然后依次判断每段是否为整数,如果是则执行命令,如果不是则直接报错退出。

文章目录
  1. 1. Command Injection
  2. 2. LOW级别
    1. 2.1. 实战
  3. 3. Medium级别
  4. 4. High级别
  5. 5. 实战
  6. 6. impossible
,